package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Poll;
import com.ericom.service.GenericManager;

public class PollAction extends BaseAction {

	private GenericManager<Poll, Long> pollManager;
	private List polls;
	private Poll poll;
	private Long id;

	public Poll getPoll() {
		return poll;
	}

	public void setPoll(Poll poll) {
		this.poll = poll;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setPollManager(GenericManager<Poll, Long> pollManager) {
		this.pollManager = pollManager;
	}

	public String list() {
		polls = pollManager.getAll();
		return SUCCESS;
	}

	public List getPolls() {
		return polls;
	}

	public String delete() {
		pollManager.remove(poll.getId());
		this.saveMessage(getText("poll.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			poll = pollManager.get(id);
		} else {
			poll = new Poll();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (poll.getId() == null);
		poll = pollManager.save(poll);

		String key = (isNew) ? "poll.added" : "poll.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
